package com.huika.renrench.control.project.activity;

import java.util.ArrayList;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.project.adapter.CaseListAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.model.ProjectCaseBean;
import com.huika.renrench.support.bean.EnshipProject;
import com.huika.renrench.view.TitleBarHelper;

public class ProjectCaseActivity extends BaseAct implements OnItemClickListener, Listener<RequestResult<ArrayList<ProjectCaseBean>>> {

	private PullToRefreshListView caseLv;
	private View nullView;
	private CaseListAdapter mAdapter;

	@Override
	public void setRootView() {
		setContentView(R.layout.project_case);
		initViews();
		initTopBar();
		initListener();
		//获取数据
		getProjectCaseData();
	}

	private void getProjectCaseData() {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();

		ajaxParams.putCommonTypeParam("pageNo", CURRENT_PAGE);
		ajaxParams.putCommonTypeParam("pageSize", PAGE_SIZE);

		FormResultRequest<ArrayList<ProjectCaseBean>> getResultRequest = new FormResultRequest<ArrayList<ProjectCaseBean>>(
				"http://192.168.16.219:8082/rrcpro-api/projectService/GetSuccProjects",
				this, this,
				new TypeToken<RequestResult<ArrayList<ProjectCaseBean>>>() {
				}.getType());
		getResultRequest.setRequestParams(ajaxParams);
		getResultRequest.setRetryPolicy(new DefaultRetryPolicy(30 * 1000, 1,
				1.0f));
		executeRequest(getResultRequest);
	}

	
	
	private void initListener() {
		caseLv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				Toast.makeText(context, "滚动到底部了", Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void initTopBar() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this,
				R.string.common_null, R.string.common_null,
				R.string.common_null);
		titleBarHelper.setTitleMsg("案例");
		titleBarHelper.setLeftMsg("创业");
	}

	private void initViews() {
		caseLv = (PullToRefreshListView) findViewById(R.id.caselv);
		nullView = findViewById(R.id.null_layout);
		mAdapter = new CaseListAdapter(getApplicationContext());
		caseLv.setAdapter(mAdapter);
		caseLv.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
	}

	@Override
	public void onResponse(RequestResult<ArrayList<ProjectCaseBean>> result) {
		caseLv.onRefreshComplete();
		if (result.flag == 1) {
			if (result != null && result.getRs() != null
					&& result.getRs().size() > 0) {
				setNullView(false);
				setListData(result.getRs());
				changeRefreshMode(result.getTotalSize());
				Log.d("chenyuan", result.getRs() + "");
			} else {
				setNullView(true);
			}
		} else {
			// showToastMsg(result.msg);
		}
		dismissLoadingDialog();
	}
	
	private void setNullView(boolean isShow) {
		if (isShow) {
			caseLv.setVisibility(View.GONE);
			nullView.setVisibility(View.VISIBLE);
		} else {
			caseLv.setVisibility(View.VISIBLE);
			nullView.setVisibility(View.GONE);
		}
	}
	
	private void setListData(ArrayList<ProjectCaseBean> rs) {
		if (CURRENT_PAGE == 1) {
			mAdapter.setGroup(rs);
		} else {
			mAdapter.addItems(rs);
		}
	}
	
	private void changeRefreshMode(int totalSize) {
		if (totalSize > CURRENT_PAGE) {
			caseLv.setMode(Mode.BOTH);
		} else {
			caseLv.setMode(Mode.PULL_FROM_START);
		}
	}
}
